<?php
  require_once('../db.php');

  $id = isset($_REQUEST['id'])?$_REQUEST['id']:0;

if(isset($_POST['uname']) &&
      !empty($_POST['uname']))
{

  $_POST = array_map('mysql_real_escape_string',$_POST);
  $_POST = array_map('strip_tags',$_POST);
  $_POST = array_map('trim',$_POST);

  pr($_POST);

##########################################

  $validExtn = array('png',
                     'jpeg',
                     'jpg',
                     'gif' );
  $mb = 1;
  $allowedFileSize = $mb * 1024 * 1024;
  $uploadDir = './../uploads';

  if(isset($_FILES['avatar']['error']) && $_FILES['avatar']['error']==0) {
    if($_FILES['avatar']['size'] < $allowedFileSize)
    {

      $fileName = $_FILES['avatar']['name'];
      //pr($fileName);
      $fileDetails = pathinfo($fileName);
      $fileExtn = $fileDetails['extension'];
      //pr($fileExtn);
      if(!in_array($fileExtn,$validExtn)) {
        $msg = "Please uplaod file with valid extn(".implode(',',$validExtn).")";
      } else {
        $src = $_FILES['avatar']['tmp_name'];
        $fileName = date('ymdhis').'_'.uniqid().'_'.$_FILES['avatar']['name'];

        if(!is_dir($uploadDir)) {
          mkdir($uploadDir,0755);
        }
        $dest = "$uploadDir/$fileName";

        $isMove = move_uploaded_file($src, $dest);
        //var_dump($isMove);
        if($isMove == true) {
          $msg = "File uploaded successfully.";
          $_POST['avatar'] = $fileName;

          ######## CRAETE THUMB ######
          require_once('../thumbnail/image.class.php');
          $thumb_dir = './../uploads/thumb';
          if(!is_dir($thumb_dir)) {
            mkdir($thumb_dir,0755);
          }
          $img = new Zubrag_image;
          $img->max_x        = 100;
          $img->max_y        = 100;
          $img->GenerateThumbFile($dest, "$thumb_dir/$fileName");
          // here $dest is treated as Source
          ############################
// remove old image from file system

unlink("$thumb_dir/$_POST[old_avatar]");
unlink("../uploads/$_POST[old_avatar]");


        } else {
          $msg = "There might be some error during upload.Please try again.";
        }
      }
    } else {
      $msg = "Please upload file with $mb MB Max";
    }
  } else if(isset($_FILES['avatar']['error'])){
      $msg = "Please upload file with $mb MB Max";
  }
  ########################################

  // There is no such column old_avatar in DB
  unset($_POST['old_avatar']);

  $insertQuery  = "UPDATE `users` SET ";
  foreach($_POST as $colName=>$value) {
    $insertQuery .= "`$colName`='$value' ,";
  }
  $insertQuery = rtrim($insertQuery,' ,');
  $insertQuery .= " WHERE `id`=$id";
  mysql_query($insertQuery);
  if(mysql_affected_rows()==1) {
    echo "Record updated successfully";
  }
  pq($insertQuery);

}


#Fetch user from Database

  $selectQuery = "SELECT * FROM `users`
                  WHERE `id`='$id'";
  pq($selectQuery);
  $result = mysql_query($selectQuery)or die(mysql_error());
  $user = mysql_fetch_assoc($result);
  pr($user);
  $isActive = $user['status']==1?'checked':'';
  $isInactive = $user['status']==0?'checked':'';
?>

<form action="" enctype="multipart/form-data" method="post">
  <input type="hidden1234" name="id" value="<?=$id?>" />
  <input type="hidden1234" name="old_avatar" value="<?=$user['avatar']?>" />

  <table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">-:Please update the details below:-</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right">Username :</td>
        <td ><input type="text" name="uname" value="<?=$user['uname']?>" placeholder="User Name" required /></td>
      </tr>
      <tr>
        <td align="right">Email :</td>
        <td ><input type="email" name="email" value="<?=@$user['email']?>" placeholder="Please add e-mail" required /></td>
      </tr>

      <tr>
        <td align="right">Avatar :</td>
        <td ><input type="file" name="avatar"  />

<a href="../uploads/<?=@$user['avatar']?>" target="_blank">
  <img src="../uploads/thumb/<?=@$user['avatar']?>" alt="User's Image" title="Profile Pic" />
</a>
        </td>
      </tr>

      <tr>
        <td align="right">Status :</td>
        <td >
<label>
  <input type="radio" name="status" value="0" <?=$isInactive?> />Inactive
</label>
<label>
  <input type="radio" name="status" value="1" <?=$isActive?> />Active
</label>
        </td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td ><input type="submit" value="Update user !!" /></td>
      </tr>
    </tbody>
  </table>

</form>
<?php
require_once('../footer.php');
